package 代码随想2._1数组;

/**
 * @author XXX
 * @date 2024-01-13 19:53
 */

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.cn/problems/spiral-matrix-ii/description/
 * 模拟
 */
public class _5螺旋矩阵II {
    public int[][] generateMatrix(int n) {
        if(n==1)
            return new int[][]{{1}};
        int[][] res = new int[n][n];
        int left = 0 , right = n-1;
        int top = 0 , bottom = n-1;
        int k = 1;
        while(left<=right || top <= bottom){
            for(int i=left;i<=right;i++)
                res[top][i] = k++;
            top++;
            if(top>bottom)
                break;
            for(int i=top;i<=bottom;i++)
                res[i][right] = k++;
            right--;
            if(right<left)
                break;
            for(int i=right;i>=left;i--)
                res[bottom][i] = k++;
            bottom--;
            if(bottom<top)
                break;
            for(int i=bottom;i>=top;i--)
                res[i][left] = k++;
            left++;
            if(left>right)
                break;
        }
        return res;
    }
}

/**
 * https://leetcode.cn/problems/spiral-matrix/description/
 */
class 螺旋矩阵{
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res = new ArrayList<>();
        if(matrix.length==0 || matrix[0].length==0)
            return res;
        int n = matrix.length , m = matrix[0].length;
        int left = 0 , right = m-1;
        int top = 0 , bottom = n-1;
        while(left<=right || top <= bottom){
            for(int i=left;i<=right;i++)
               res.add(matrix[top][i]);
            top++;
            if(top>bottom)
                break;
            for(int i=top;i<=bottom;i++)
                res.add(matrix[i][right]);
            right--;
            if(right<left)
                break;
            for(int i=right;i>=left;i--)
                res.add(matrix[bottom][i]);
            bottom--;
            if(bottom<top)
                break;
            for(int i=bottom;i>=top;i--)
                res.add(matrix[i][left]);
            left++;
            if(left>right)
                break;
        }
        return res;
    }
}

/**
 * https://leetcode.cn/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/description/
 */
class 螺旋遍历二维数组{
    public int[] spiralArray(int[][] array) {
        List<Integer> res = new ArrayList<>();
        if(array.length==0 || array[0].length==0)
            return new int[]{};
        int n = array.length , m = array[0].length;
        int left = 0 , right = m-1;
        int top = 0 , bottom = n-1;
        while(left<=right || top <= bottom){
            for(int i=left;i<=right;i++)
                res.add(array[top][i]);
            top++;
            if(top>bottom)
                break;
            for(int i=top;i<=bottom;i++)
                res.add(array[i][right]);
            right--;
            if(right<left)
                break;
            for(int i=right;i>=left;i--)
                res.add(array[bottom][i]);
            bottom--;
            if(bottom<top)
                break;
            for(int i=bottom;i>=top;i--)
                res.add(array[i][left]);
            left++;
            if(left>right)
                break;
        }
        int[] r = new int[res.size()];
        for(int i=0;i<res.size();i++)
            r[i] = res.get(i);
        return r;
    }
}